package com.zj.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAdvice {

    @Pointcut("execution(* com.zj.transactionspringboot01.service.*.add(..))")
    public void pj1() {
    }

    @Pointcut("execution(* com.zj.transactionspringboot01.service.*.sum(..))")
    public void pj2() {
    }

    @Pointcut("execution(* com.zj.transactionspringboot01.service.*.*(..)) && @annotation(com.zj.ActionType)")
    public void pj3() {
    }

    @Before("pj1()")
    public void myBefore(JoinPoint joinPoint) {
        System.out.println("前置通知" + joinPoint.getSignature());
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println("参数：" + arg);
        }

    }

    @After(value = "pj3()")
    public void myAfter(JoinPoint joinPoint) {
        System.out.println("后置必定通知");
    }

    @AfterReturning(value = "pj3()", returning = "returnValue")
    public void myAfterReturning(JoinPoint joinPoint, Object returnValue) {
        System.out.println("后置返回通知");
        System.out.println("返回值：" + returnValue);

    }

    @AfterThrowing(value = "pj2()", throwing = "ex")
    public void myThrow(JoinPoint joinPoint, Exception ex) {
        System.out.println(joinPoint.getSignature() + "异常通知：" + ex.getMessage());
    }

    @Around("pj2()")
    public Object myAround(ProceedingJoinPoint pp) {
        Object[] args = pp.getArgs();
        Object obj = null;
        System.out.println("环绕通知前,args.size:" + args.length);
        for (Object arg : args) {
            System.out.println("参数：" + arg);
        }
        try {
            obj = pp.proceed();
            System.out.println("环绕通知后,返回值:" + obj);

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return obj;

    }
}
